﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_HosptialUpdate : System.Web.UI.Page
{
    private Int64 AccountId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            AccountId = Int64.Parse(SysFun.IsSQL(Request.QueryString["AccountId"]));

            if (!this.IsPostBack)
            {
                HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
                HZ.MBSM.Model.Hospital Hospital = (HZ.MBSM.Model.Hospital)oo.Retrieve(typeof(HZ.MBSM.Model.Hospital), AccountId);
                HZ.MBSM.Model.Area Area = (HZ.MBSM.Model.Area)oo.Retrieve(typeof(HZ.MBSM.Model.Area), Hospital.AreaId);

                if (Hospital != null)
                {
                    if (Area != null)
                        this.Label1.Text = "<input  InvertedComma='地区' type='text' name='AreaName' style='width: 97px' value='" + Area.AreaName + "' /><input type='text' name='AreaId' style='display:none; width: 64px;' value='" + Hospital.AreaId + "'>";
                    else
                        this.Label1.Text = "<input  InvertedComma='地区' type='text' name='AreaName' style='width: 97px' /><input type='text' name='AreaId' style='display:none; width: 64px;' >";
                    txtUserCode.Text = SysFun.ToTrim(Hospital.UserCode);
                    HospitalName.Text = Hospital.HospitalName;
                    IsOldLeader.SelectedValue = SysFun.BoolToByte(Hospital.IsOldLeader).ToString();
                    txtLawer.Text = Hospital.Lawer;
                    PostCode.Text = Hospital.PostCode;
                    RegistDate.Text = Hospital.RegistDate.ToShortDateString();
                    RegistDate.Visible = false;
                    Telephone.Text = Hospital.Telephone;
                    Sort.SelectedValue = Hospital.Sort;
                    Address.Text = Hospital.Address;
                    distinction.SelectedValue = Hospital.distinction;
                    txtbedNum.Text = Hospital.bedNum.ToString();
                    txtEmail.Text = Hospital.Email;
                    txtFax.Text = Hospital.Fax;
                    txtmemo1.Text = Hospital.memo1;
                    txtLinkMan.Text = Hospital.LinkMan;
                }

                oo.Dispose();
                db.Close();
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        HZ.MBSM.DBUtility.Db db = new HZ.MBSM.DBUtility.Db(false);        
        Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
        HZ.MBSM.Model.Hospital Hospital = new HZ.MBSM.Model.Hospital();

        if (Hospital != null)
        {
            int rowCun = 0;
            Db db1 = new Db();
            System.Data.IDataReader dr;
            String strSql = "select count(accountid) as cun from dbo.v_account where (dwname= '" + SysFun.ToTrim(HospitalName.Text) + "' or usercode='" + SysFun.ToTrim(txtUserCode.Text) + "') AND AccountId<>" + AccountId.ToString();
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该用户在数据库中已经存在！！！');</script>");
            }
            else
            {

                Hospital.AccountId = AccountId;
                Hospital.UserCode = SysFun.ToTrim(txtUserCode.Text);
                Hospital.Address = Address.Text;
                Hospital.AreaId = SysFun.ToInt(Request.Form["AreaId"]);
                Hospital.HospitalName = HospitalName.Text;
                Hospital.IsOldLeader = Convert.ToBoolean(int.Parse(IsOldLeader.SelectedValue));
                Hospital.Lawer = txtLawer.Text;
                Hospital.PostCode = PostCode.Text;
                Hospital.RegistDate = Convert.ToDateTime(RegistDate.Text);
                Hospital.Telephone = Telephone.Text;
                Hospital.Sort = Sort.SelectedValue;
                Hospital.distinction = distinction.SelectedValue;
                Hospital.Email = txtEmail.Text;
                Hospital.bedNum = SysFun.ToInt(txtbedNum.Text);
                Hospital.memo1 = txtmemo1.Text;
                Hospital.LinkMan = txtLinkMan.Text;
                Hospital.Fax = txtFax.Text;
                oo.Update(Hospital);
                oo.Dispose();
                db.Close();
                Response.Write("<script>alert('保存成功！！！');</script>");
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "Hospital", Hospital.AccountId + "|" + Hospital.HospitalName + "|" + Hospital.UserCode + "|" + Hospital.AreaId, "../Magage/HospitalUpdate.aspx");

                Response.Write("<script>window.opener.location.reload();window.close();</script>");
            }
        }

        oo.Dispose();
        db.Close();
    }


}
